var fafang_status = [ {id : '1',text : '已领取'},{id : '2',text : '未领取'}];
var users = LG.getSelectData(basepath+"user/getUsers.do");
var classes = LG.getSelectData(basepath+"user/getClasses.do");
$(function(){
	// 列表结构
	 var grid = $("#maingrid").ligerGrid(
					{
						columns : [{display : "班级",name : "class_",width : 120,type : "text",align : "center", 
							render: function(item){
								var html = '';
								for (var i = 0; i < classes.length; i++){
							        if (classes[i]['id'] == item['class_']){
							        	html = classes[i]['text'];
							        	break;
							        }
							    }
								return html;
							}}, {display : "领取人",name : "name",width : 100,type : "text",align : "center"},
						            {display : "军服",name : "junfu",width : 120,type : "text",align : "center", 
												render: function(item){
													var html = '';
													for (var i = 0; i < fafang_status.length; i++){
												        if (fafang_status[i]['id'] == item['junfu']){
												        	if(fafang_status[i]['id'] > 1){
												        		html = "<span style='color:red'>"+fafang_status[i]['text']+"</span>";
												        	}else{
												        		html = fafang_status[i]['text'];
												        	}
												        	break;
												        }
												    }
													return html;
												}},
												{display : "军帽",name : "junmao",width : 120,type : "text",align : "center", 
													render: function(item){
														var html = '';
														for (var i = 0; i < fafang_status.length; i++){
													        if (fafang_status[i]['id'] == item['junmao']){
													        	if(fafang_status[i]['id'] > 1){
													        		html = "<span style='color:red'>"+fafang_status[i]['text']+"</span>";
													        	}else{
													        		html = fafang_status[i]['text'];
													        	}
													        	break;
													        }
													    }
														return html;
													}},
													{display : "军鞋",name : "junxie",width : 120,type : "text",align : "center", 
														render: function(item){
															var html = '';
															for (var i = 0; i < fafang_status.length; i++){
														        if (fafang_status[i]['id'] == item['junxie']){
														        	if(fafang_status[i]['id'] > 1){
														        		html = "<span style='color:red'>"+fafang_status[i]['text']+"</span>";
														        	}else{
														        		html = fafang_status[i]['text'];
														        	}
														        	break;
														        }
														    }
															return html;
														}},
														{display : "徽章",name : "huizhang",width : 120,type : "text",align : "center", 
															render: function(item){
																var html = '';
																for (var i = 0; i < fafang_status.length; i++){
															        if (fafang_status[i]['id'] == item['huizhang']){
															        	if(fafang_status[i]['id'] > 1){
															        		html = "<span style='color:red'>"+fafang_status[i]['text']+"</span>";
															        	}else{
															        		html = fafang_status[i]['text'];
															        	}
															        	break;
															        }
															    }
																return html;
															}}
										  ],
						dataAction : 'server',
						pageSize : 20,
						sortName : "id",
						delayLoad:true,
						toolbar : {},
						url : basepath + 'SystemWeb/getGrid.do?resource=getArmyGoods',
						width : '98%',
						height : '100%',
						heightDiff : -10,
						rownumbers:false,
						checkbox : false
					});
	 $("#formsearch").ligerForm({
		   fields:[
		           {display:"班级",name:"classname",newline:true,labelWidth:100,width:220,space:30,type:"text",cssClass:"field",attr: { op: "like" }}
		            ],
		   toJSON: JSON2.stringify
	  });
	 $(".l-panel-bbar-inner").hide();
	 $(".l-panel-bar").hide();
	//增加查询/重置按钮,并创建事件
		LG.appendSearchButtons("#formsearch", grid);
	 grid.loadData(true);
	// 加载toolbar
	var items = [];
	items.push({ text: '发放',id:'fafang',click: toolbarBtnItemClick, img: basepath+"resource/icons/miniicons/page_edit.gif" });
	items.push({ text: '修改',id:'update',click: toolbarBtnItemClick, img: basepath+"resource/icons/miniicons/page_edit.gif" });
	grid.toolbarManager.set('items', items);
	// 工具条事件
	  function toolbarBtnItemClick(item) {
			switch (item.id) {
			case "fafang":
				showDetail({
					id:'',
					auth_id:'',
					name:'',
					junfu:'',
					junmao:'',
					junxie:'',
					huizhang:''
				},true,false);
				break;
			case "update" :
				var selected = grid.getSelected();
				if(!selected){
					LG.tip("请选择行");
					return
				}
				showDetail({
					id:selected.id,
					auth_id:selected.auth_id,
					name:selected.name,
					junfu:selected.junfu,
					junmao:selected.junmao,
					junxie:selected.junxie,
					huizhang:selected.huizhang
			},false,false);
				break;
	  }
	  var detailWin = null, curentData = null, isView=null,isAdd=null;
		function showDetail(data,isAdd,isView) {
			curentData = data;
			var mainform4room ;
			isView = isView;
			isAdd = isAdd;
				// 创建表单结构
				mainform4room = $("#mainform4meeting");
				mainform4room.ligerForm({
					inputWidth: 280,
		    		fields :   [
		    		            {display : "id",name : "id",newline : true,labelWidth : 120,width : 220,space : 20,type : "hidden"},
		    					isAdd?{display : "学生姓名",name : "name",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "name_",options: { valueFieldID: "name", data: LG.getSelectData(basepath+"armyGoods/getNames.do")},validate : {required : true}}:{display : "学生姓名",name : "name",newline : true,labelWidth : 120,width : 220,space : 20,type : "text"},
		    					{display : "auth_id",name : "auth_id",newline : true,labelWidth : 120,width : 220,space : 20,type : "hidden"},		
		    					{display : "军服",name : "junfu",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "junfu_",options: { valueFieldID: "junfu", data: fafang_status},validate : {required : true}},
		    					{display : "军帽",name : "junmao",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "tzs_",options: { valueFieldID: "junmao", data: fafang_status},validate : {required : true}},
		    					{display : "军鞋",name : "junxie",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "ref_doc_",options: { valueFieldID: "junxie", data: fafang_status},validate : {required : true}},
		    					{display : "徽章",name : "huizhang",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "letter_",options: { valueFieldID: "huizhang", data: fafang_status},validate : {required : true}}
		    							],
		    		onAfterSetFields : function(){$(".l-group",mainform4room).width(1050);},
		    		readonly:isView,
					toJSON:JSON2.stringify
		    });
				
				var detailWin = $.ligerDialog.open({
					target : $("#detailwindow4meeting"),
					width : 800,
					height : 520,
					title:'军鞋物资',
					top : 0,
					buttons : [ {
						text : '确定',
						onclick : function() {
							if(!isView){
								saveMeeting();
							}else{
								detailWin.hide();
							}
						}
					}, {
						text : '取消',
						onclick : function() {
							detailWin.hide();
						}
					} ]
				});
			if (curentData) {
				LG.loadDataForm(mainform4room,curentData);
			}
			function saveMeeting() {
				jQuery.metadata.setType("attr", "validate"); 
				 LG.validate(mainform4room, { debug: true });
				 if(mainform4room.valid()){
					 var savedata = {};
				    	savedata.id = $("#id").val();
				    	// 员工信息
				    	savedata.junfu = $("#junfu").val();
				    	savedata.junmao = $("#junmao").val();
				    	savedata.junxie = $("#junxie").val();
				    	savedata.huizhang = $("#huizhang").val();
				    	if(!isAdd&&!isView){
				    		savedata.auth_id = $("#auth_id").val();
				    	}else{
				    		savedata.auth_id = $("#name").val();
				    	}
						LG.ajax({
							loading : '正在保存数据中...',
							url: basepath+"armyGoods/"+(isAdd ? "insert.do"
									: "update.do"),
							data : savedata,
							success : function(data,Message) {
								LG.tip(Message);
									detailWin.hide();
									f_reload();
								
							},
							error : function(message) {
								LG.tip(message);
							}
						});
				 }
			}
		}
	  
	function f_reload() {
		grid.loadData();
	}
}
}  
);




 